#include <iostream>
#include <pthread.h>
#include <unistd.h>

int count = 1;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *test(void *args)
{
    std::string name = static_cast<char *>(args);

    while (count > 0)
    {
        pthread_mutex_lock(&mutex);
        std::cout << name << "出售 : " << count << std::endl;
        count--;
        usleep(1000);
        pthread_mutex_unlock(&mutex);
    } 
    return args;
}

int main()
{

    return 0;
}
// int count = 1;
// pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
// void *task(void *args)
// {
//     std::string name = static_cast<char *>(args);
//     while (true)
//     {
//         pthread_mutex_lock(&mutex);
//         if (count <= 0)
//         {
//             pthread_cond_wait(&cond, &mutex);
//         }
//         count--;
//         std::cout << name << ",count =  " << count << std::endl;
//         sleep(1);
//         count++;
//         pthread_cond_signal(&cond);
//         pthread_mutex_unlock(&mutex);
//         usleep(100);
//     }
//     return args;
// }

// int main()
// {
//     pthread_t t1, t2, t3, t4;
//     pthread_create(&t1, nullptr, task, (void *)"thread-1");
//     pthread_create(&t2, nullptr, task, (void *)"thread-2");
//     pthread_create(&t3, nullptr, task, (void *)"thread-3");
//     pthread_create(&t4, nullptr, task, (void *)"thread-4");

//     pthread_join(t1, nullptr);
//     pthread_join(t2, nullptr);
//     pthread_join(t3, nullptr);
//     pthread_join(t4, nullptr);

//     sleep(1);
//     return 0;
// }